package com.ogawa.joypal.user.util;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.widget.TextView;

public class CountDownUtil extends CountDownTimer {
    private TextView tvCode;
    private String tvCodeText;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownUtil(long millisInFuture, long countDownInterval, TextView tvCode) {
        super(millisInFuture, countDownInterval);
        this.tvCode = tvCode;
        tvCodeText = tvCode.getText().toString();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onTick(long millisUntilFinished) {
        tvCode.setText(tvCodeText + "(" + millisUntilFinished / 1000 + ")");
        tvCode.setEnabled(false);
    }

    @Override
    public void onFinish() {
        tvCode.setEnabled(true);
        tvCode.setText(tvCodeText);
    }
}
